package lib.quasar.widget.text;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import lib.quasar.widget.R;

/**
 * description: 两侧文字
 * created by kalu on 2018/4/10 15:16
 */
public class SideTextView extends View {

    private final Paint mPaint = new Paint();
    private boolean localtionTop = false;
    private boolean localtionBottom = false;
    private boolean localtionLeft = false;
    private boolean localtionRight = false;
    private boolean paintBold = false;
    private float paddingTop = 0;
    private float paddingBottom = 0;
    private float paddingLeft = 0;
    private float paddingRight = 0;
    private float lineHeight = 5;
    private int lineColor = Color.BLACK;
    private String leftStr;
    private String rightStr;
    private float textSize = 10;

    public SideTextView(Context context) {
        this(context, null, 0);
    }

    public SideTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SideTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = null;

        try {

            array = context.obtainStyledAttributes(attrs, R.styleable.SideTextView);

            localtionTop = array.getBoolean(R.styleable.SideTextView_stv_location_top, false);

            localtionTop = array.getBoolean(R.styleable.SideTextView_stv_location_top, false);
            localtionBottom = array.getBoolean(R.styleable.SideTextView_stv_location_bottom, false);
            localtionLeft = array.getBoolean(R.styleable.SideTextView_stv_location_left, false);
            localtionRight = array.getBoolean(R.styleable.SideTextView_stv_location_right, false);
            paintBold = array.getBoolean(R.styleable.SideTextView_stv_paint_bold, false);

            paddingTop = array.getDimension(R.styleable.SideTextView_stv_padding_top, 0);
            paddingBottom = array.getDimension(R.styleable.SideTextView_stv_padding_bottom, 0);
            paddingLeft = array.getDimension(R.styleable.SideTextView_stv_padding_left, 0);
            paddingRight = array.getDimension(R.styleable.SideTextView_stv_padding_right, 0);

            lineHeight = array.getDimension(R.styleable.SideTextView_stv_line_height, lineHeight);
            lineColor = array.getColor(R.styleable.SideTextView_stv_line_color, lineColor);

            leftStr = array.getString(R.styleable.SideTextView_stv_text_left);
            rightStr = array.getString(R.styleable.SideTextView_stv_text_right);
            textSize = array.getDimension(R.styleable.SideTextView_stv_text_size, textSize);

        } catch (Exception e) {
            //   LogUtil.e("LineTextView", e.getMessage(), e);
        } finally {
            if (null != array) {
                array.recycle();
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if (paintBold) {
            mPaint.setFakeBoldText(true);
        }

        super.onDraw(canvas);

        int height = getHeight();
        int width = getWidth();

        mPaint.setAntiAlias(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setColor(lineColor);

        if (localtionTop) {
            canvas.drawRect(paddingLeft, 0, width - paddingRight, lineHeight, mPaint);
        }

        if (localtionBottom) {
            canvas.drawRect(paddingLeft, height - lineHeight, width - paddingRight, height, mPaint);
        }

        if (localtionLeft) {
            canvas.drawRect(paddingLeft, paddingTop, lineHeight, height - paddingBottom, mPaint);
        }

        if (localtionRight) {
            canvas.drawRect(width - paddingRight - lineHeight, paddingTop, width - paddingRight, height - paddingBottom, mPaint);
        }

        if (!TextUtils.isEmpty(leftStr)) {
            mPaint.setTextAlign(Paint.Align.LEFT);
            mPaint.setColor(Color.BLACK);
            mPaint.setTextSize(textSize);
            final float font = mPaint.getFontMetrics().bottom - mPaint.getFontMetrics().top;
            canvas.drawText(leftStr, getPaddingLeft(), getHeight() / 2 + font / 3, mPaint);
        }

        if (!TextUtils.isEmpty(rightStr)) {
            mPaint.setTextAlign(Paint.Align.RIGHT);
            mPaint.setColor(Color.BLACK);
            mPaint.setTextSize(textSize);
            final float font = mPaint.getFontMetrics().bottom - mPaint.getFontMetrics().top;
            canvas.drawText(rightStr, getWidth() - getPaddingRight(), getHeight() / 2 + font / 3, mPaint);
        }
    }

    public void setText(String left, String right) {
        leftStr = left;
        rightStr = right;
        postInvalidate();
    }

    public void setTextLeft(String left) {
        leftStr = left;
        postInvalidate();
    }

    public void setTextRight(String right) {
        rightStr = right;
        postInvalidate();
    }
}